Component({
  options: {
    multipleSlots: true,
    addGlobalClass: true
  },
  properties: {
    count: {
      type: Number,
      value: 1
    },
    inventory: {
      type: Number,
      value: 0
    },
    perLimitNum:{
      type:Number,
      value:0
    },
    collItemkey:{
      type:Number,
      value:0
    },
    leastNum: {
      type: Number,
      value: 0,
      observer:function (newVal,oldVal){
        this.setData({showNum:newVal})
      }
    },
  },

// observers:{
//     "showNum":function (newVal){
//       this.triggerEvent('changeCollNum',{collItemkey:this.data.collItemkey,showNum:this.data.showNum})
//  debugger
//     }
// },
  data: {
    showNum: 1,
    timeoutId: null,
    lastNumber:0,
    minNum:1
  },
  pageLifetimes:{
    show:function (){
        this.setData({showNum:this.data.leastNum||1})
    }
  },
  lifetimes:{
    attached() {
      this.lastNumberFn()
    }
  },
  methods: {
    handleInput(event) {
      let val = event.detail.value;
      if ( val >this.data.lastNumber&&this.data.lastNumber!==0) {
        wx.showToast({title: `限制购买${this.data.lastNumber}件`, icon: "none", duration: 2000});
        this.setData({showNum: this.data.lastNumber});
      }else if (val < this.data.leastNum) {
        wx.showToast({title: `最少必须购买${this.data.leastNum}件哦！`, icon: "none", duration: 2000});
        this.setData({showNum: this.data.leastNum});
      }else {
        this.setData({showNum: Number(val)})
      }
        this.triggerEvent('changeCollNum',{collItemkey:this.data.collItemkey,showNum:this.data.showNum})

      // this.triggerEvent('update', {num: Number(val)});
    },
    lastNumberFn(){
      var lastNumber=this.data.lastNumber;
      if(this.data.inventory===0){

      }else {
        // lastNumber=this.data.perLimitNum===0?this.data.inventory:(this.data.perLimitNum<=this.data.inventory?this.data.perLimitNum:this.data.inventory)
        if(this.data.perLimitNum===0){
          lastNumber = this.data.inventory
        }else {
          lastNumber = this.data.perLimitNum<=this.data.inventory?this.data.perLimitNum:this.data.inventory
        }
      }
      this.setData({lastNumber:lastNumber})
    },
    handleAdd() {
      let showNum = this.data.showNum
      this.lastNumberFn()
      if (showNum>=this.data.lastNumber&&this.data.lastNumber!==0){
        wx.showToast({title:"超出最大购买数量",icon:"none",duration:2000})
        this.setData({showNum:this.data.lastNumber})
        return;
      }
      showNum = showNum + 1
      this.setData({showNum: showNum});
      // 限流
      this.throttleEvent(showNum);
      this.triggerEvent('changeCollNum',{collItemkey:this.data.collItemkey,showNum:this.data.showNum})

    },

    handleSubtract() {
      let showNum = this.data.showNum
      const minNum =this.data.leastNum===0?1:this.data.leastNum;
      this.setData({minNum})
      if (showNum - 1 <minNum) {
        wx.showToast({
          title: '该商品不能减少了噢',
          icon: 'none'
        })
        this.setData({showNum:minNum})
      }else {
        showNum = showNum - 1
        this.setData({showNum: showNum});
        this.throttleEvent(showNum);
        this.triggerEvent('changeCollNum',{collItemkey:this.data.collItemkey,showNum:this.data.showNum})
      }


    },
    throttleEvent(showNum) {
      let timeoutId = this.data.timeoutId
      if (timeoutId) {
        this.setData({timeoutId: null})
        clearTimeout(timeoutId);
      }
      timeoutId = setTimeout(() => {
        this.triggerEvent('update', {num: showNum});
      }, 100)
      this.setData({timeoutId: timeoutId})
    }
  }
});
